package com.dc.controller;

import POJO.Person;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.alibaba.fastjson.JSON.toJSONString;

@Controller
public class PersonController {

    @RequestMapping("/toPerson")
public  String toPerson(Person person){
    System.out.println(person.getName()+
            ""+person.getAge());
    return "";
}

/*
    public void initBinder(ServletRequestDataBinder binder){
    binder.registerCustomEditor(Date.class,
              new CustomDateEditor(
          new SimpleDateFormat(
       "yyyy-MM-dd HH:mm"),
       true));

}*/

@RequestMapping("/show")
public String  showPerson(Map<String,Object> map) throws  Exception{
    Person person=new Person();
    map.put("person",person);
    person.setAge(18);
    person.setName("sdfkjg");
    person.setCreate_time(new Date());

    return "views/Person.jsp";

}
@RequestMapping(value = "getTest",method = RequestMethod.POST)
public void getText(@RequestParam String name, HttpServletResponse response)throws Exception{
    response.setContentType("utf-8");
    response.setContentType("application/json;charset=utf-8");
    PrintWriter printWriter=response.getWriter();
    printWriter.print("姓名"+name);
}

@RequestMapping("/path/{name}")
@ResponseBody
public Person path(
        @PathVariable("name")String n,
                 HttpServletResponse response) throws Exception{
    response.setContentType("utf-8");
    response.setContentType("application/json;charset=utf-8");
    Map<String,Object>map=new HashMap<String, Object>();
    map.put("name",n);
 Person person=new Person();
 person.setName(n);
    return person;

    //  PrintWriter printWriter=response.getWriter();
   // printWriter.print(JSON,toJSONString(map));

    }


}
